#!/bin/bash

SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

RACO="/Applications/Racket v5.3/bin/raco"

cd "$DIR"
rm -rfv bin/mac
mkdir bin/mac
cd src
"$RACO" exe --gui --icns ../artwork/kirisurf.icns main.rkt
"$RACO" distribute MAC main.app
#exit
rm -rfv main.app
mv MAC/main.app ../bin/mac/Kirisurf.app
mv ../bin/mac/Kirisurf.app/Contents/MacOS/main ../bin/mac/Kirisurf.app/Contents/MacOS/main_real
cp mac_stub ../bin/mac/Kirisurf.app/Contents/MacOS/main
chmod +x ../bin/mac/Kirisurf.app/Contents/MacOS/main
cp -R macosx certificate.pem ../bin/mac/Kirisurf.app/Contents/MacOS/
cp kiri.png ../bin/mac/Kirisurf.app/Contents/MacOS/
rm -rfv *.app
rm -rfv MAC
